<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" :model="queryParams" ref="ruleFormRef">
        <el-form-item label="岗位编码" prop="postCode">
          <el-input
            v-model="queryParams.postCode"
            placeholder="请输入岗位编码"
            clearable
            style="width: 200px"
            @keyup.enter.native="getPostListPage"
          />
        </el-form-item>
        <el-form-item label="岗位名称" prop="postName">
          <el-input
            v-model="queryParams.postName"
            placeholder="请输入岗位名称"
            clearable
            style="width: 200px"
            @keyup.enter.native="getPostListPage"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="岗位状态"
            clearable
            style="width: 100px"
            @keyup.enter.native="getPostListPage"
          >
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getPostListPage">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="footer">
      <div class="util">
        <el-button
          type="primary"
          plain
          icon="CirclePlus"
          @click="addHandler"
          v-hasPermi="['system:post:add']"
        >
          新增
        </el-button>

        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete(ids)"
          v-hasPermi="['system:role:edit']"
        >
          批量删除
        </el-button>

        <el-button type="warning" plain icon="Download"> 导出 </el-button>
      </div>

      <div class="table-inner">
        <el-table
          v-if="refreshTable"
          :default-expand-all="isExpandAll"
          v-loading="loading"
          row-key="deptId"
          :data="tableData"
          style="width: 100%"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column
            label="岗位编号"
            prop="postId"
            width="120"
            align="center"
          ></el-table-column>
          <el-table-column
            label="权限编码"
            prop="postCode"
            width="150"
            align="center"
          ></el-table-column>
          <el-table-column
            label="岗位名称"
            prop="postName"
            width="150"
            align="center"
          ></el-table-column>
          <el-table-column
            label="显示顺序"
            prop="postSort"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template #default="scope">
              <DictTag :options="dict.type.sys_normal_disable" :value="scope.row.status"></DictTag>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
            how-overflow-tooltip
            width="180"
          ></el-table-column>
          <el-table-column prop="operator" label="操作" width="200px" align="center">
            <template #default="scope">
              <template v-if="scope.row.roleId != 1">
                <el-button type="primary" size="small" icon="Edit" @click="editHandler(scope.row)">
                  编辑
                </el-button>
                <el-button
                  @click="handleDelete(scope.row)"
                  type="danger"
                  size="small"
                  icon="Delete"
                >
                  删除
                </el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <!--分页器            -->
        <el-pagination
          style="margin-top: 40px"
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :background="true"
          layout="prev, pager, next, jumper, ->,  sizes, total"
          :total="total"
          @current-change="getPostListPage"
          @size-change="getPostListPage"
        />
      </div>
    </div>
    <!--对话框    -->
    <postDialog ref="postDialogRef" @getPostListPage="getPostListPage" />
  </div>
</template>

<script lang="ts" setup>
  import { ElMessageBox, ElMessage, FormInstance } from 'element-plus'
  import { getCurrentInstance, nextTick, onMounted, reactive, ref } from 'vue'
  import { reqPostDeleteUrl, reqPostPageListUrl } from '@/api/system/post'
  import DictTag from '@/components/DictTags/index.vue'
  import PostDialog from './components/postDialog.vue'

  const tableData = ref() /*表格的数据*/
  const loading = ref(true) // 遮罩层
  const dialogVisible = ref(false)
  const ruleFormRef = ref<FormInstance>()
  const refreshTable = ref(true) // 重新渲染表格状态
  const isExpandAll = ref(true) // 是否展开，默认全部折叠
  const total = ref(0) /*总计*/
  const postDialogRef = ref() /*对话框ref*/
  // 非单个禁用---判断修改按钮是否可用
  const single = ref(true)
  // 非多个禁用---判断删除按钮
  const multiple = ref(true)
  // 多选框选中数据
  const ids = ref([]) // 非多个禁用
  const { proxy }: any = getCurrentInstance()
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    postName: '',
    postCode: '',
    status: '' /*状态*/,
  })

  onMounted(async () => {
    await getPostListPage() //获取第一页的信息
    setTimeout(async () => {
      loading.value = false
    }, 500)
  })

  const getPostListPage = async () => {
    console.log('submit!', queryParams)
    loading.value = true
    setTimeout(async () => {
      await reqPostPageListUrl(queryParams).then((req: any) => {
        console.log(req)
        if (req.code == 200) {
          tableData.value = req.data
          total.value = req.total
        } else {
          console.log('查询失败！')
          console.log(req)
        }
      })
      loading.value = false
    }, 500)
  }

  //重置
  const resetQuery = (formEl: FormInstance | undefined) => {
    loading.value = true
    proxy.$resetForm(formEl)
    queryParams.postCode = ''
    queryParams.postName = ''
    queryParams.status = ''
    /*再次查询*/
    getPostListPage() //获取第一页的信息
    setTimeout(() => {
      loading.value = false
    }, 500)
  }

  //添加部门
  const addHandler = () => {
    postDialogRef.value?.show()
  }
  //修改部门
  const editHandler = (row) => {
    postDialogRef.value?.show(row)
  }

  const handleDelete = (row) => {
    const postIds = row.postId || ids.value
    proxy.$modal
      .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
      .then(async () => {
        await reqPostDeleteUrl(postIds).then(async (req: any) => {
          if (req.code == 200) {
            await getPostListPage()
            proxy.$modal.msgSuccess('删除成功')
          } else {
            proxy.$modal.msgError(req.message != null ? req.message : '删除成功')
          }
        })
      })
      .catch(() => {})
  }

  /*多选框*/
  const handleSelectionChange = (selection: any) => {
    console.log('selection', selection)
    ids.value = selection.map((item: any) => item.postId)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }
</script>

<script lang="ts">
  export default {
    dicts: ['sys_normal_disable'],
  }
</script>

<style scoped lang="scss">
  @import './index';
</style>
